import java.util.*;

public class Main {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(Arrays.toString(s.intersection(new int[]{4,9,5}, new int[]{9,4,9,8,4})));
    }
}


class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> s = new HashSet<>();
        Set<Integer> ans = new HashSet<>();
        for(int each: nums1){
            s.add(each);
        }
        for(int each: nums2){
            if(s.contains(each)){
                ans.add(each);
            }
        }
        int[] ret = new int[ans.size()];
        int i = 0;
        for(int each: ans){
            ret[i++] = each;
        }

        return ret;
    }
}